package com.jrx.anytxn.reporting.job.lsSecondFile.config;

import com.jrx.anytxn.reporting.config.LSFileConfig;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.reportingFile.config.MergeReportingFileConfiguration;
import com.jrx.anytxn.reporting.service.ILSFileService;
import com.jrx.anytxn.reporting.service.ILSSecondFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 临商二代征信文件合并上传
 *
 * @author liuyang
 * @date 2021-04-26
 */
@Configuration
public class MergeLSSecondFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(MergeReportingFileConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private LSFileConfig fileConfig;
    @Autowired
    private ILSSecondFileService secondFileService;
    @Autowired
    private ILSFileService fileService;

    @Bean
    public Job mergeLSSecondFileJob(){
        return jobBuilderFactory.get("mergeLSSecondFileJob")
                .start(mergeLSSecondFileStep())
                .next(transLSSecondFileStep())
                .build();
    }

    @Bean
    public Step mergeLSSecondFileStep() {
        return stepBuilderFactory.get("mergeLSSecondFileStep")
                .tasklet(mergeLSSecondFileStepTasklet())
                .build();
    }

    @Bean
    public Step transLSSecondFileStep() {
        return stepBuilderFactory.get("transLSSecondFileStep")
                .tasklet(transLSSecondFileStepTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet mergeLSSecondFileStepTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.info("临商二代征信文件合并开始");
            secondFileService.mergeLSSecondFile(fileConfig.getLsSecondFilePath(), fileConfig.getLsSecondOutPath());
            logger.info("临商二代征信文件合并处理完成");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    @StepScope
    public Tasklet transLSSecondFileStepTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.info("临商二代征信文件传输至NTFS开始");
            fileService.transLSFile(fileConfig.getLsSecondOutPath(), fileConfig.getLsNftsPath(), ReportingConstant.NTFS_LS_2_ZXBS);
            logger.info("临商二代征信文件传输至NTFS完成");
            return RepeatStatus.FINISHED;
        };
    }
}
